package com.base.cn.platform.os.controller.integragoods;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.course.CourseType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.integralgood.IntegralExchangeRecordExpand;
import com.base.cn.platform.os.entity.integralgood.IntegralGood;
import com.base.cn.platform.os.service.manage.integralgood.IntegralExchangeRecordService;
import com.base.cn.platform.os.service.manage.integralgood.IntegralGoodService;
import io.reactivex.internal.util.BlockingIgnoringReceiver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Hello豆包
 * @Date: 2019/10/18 9:32
 */
@Controller
@RequestMapping("/admin/IntegralGood/")
public class IntegralGoodController extends AdminBaseController {
    @Autowired
   private IntegralGoodService integralGoodService;
    @Autowired
    private IntegralExchangeRecordService integralExchangeRecordService;

    /**
     * 初始化兑换商品页面
      * @return
     */
    @RequestMapping(value = {"initAddGood","initUpdateGood"})
    public  String initGood(Model model, @RequestParam(value = "id",required =  false,defaultValue = "0")BigDecimal id){
            Map  entity =new HashMap();
            if(DataUtil.idIsNotNull(id)){
                entity=   integralGoodService.findByIdEntity(id,true);
            }
            model.addAttribute("entity",entity);
        return  resultPage("/integralgood/initgood");
    }

    /**
     *保存积分兑换商品
     * @param entity
     * @return
     */
    @RequestMapping("saveEntity")
    @ResponseBody
    public Map save(@RequestBody IntegralGood entity){
        Map<String, Object> map = integralGoodService.saveEntity(entity);
        return  map;
    }


    /**
     * 积分兑换商品列表
     * @return
     */
    @RequestMapping("listPage")
    public  String listPage(){
        return  resultPage("/integralgood/good-list");
    }

    /**
     * 积分兑换商品列表
     * @return
     */
    @RequestMapping("ajaxListPage")
    public  String ajaxListPage(Model model,@RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage , @RequestBody IntegralGood integralGood){
        Map listPage = integralGoodService.findListPage(currentPage, integralGood);
        model.addAttribute("dataMap",listPage);
        return  resultPage("/integralgood/ajax-good-list");
    }


    /**
     * 积分兑换商品列表
     * @return
     */
    @RequestMapping("updateStatus")
    @ResponseBody
    public  Map updateStatus(@RequestParam("ids") String ids,@RequestParam("status") Integer status){
        return  integralGoodService.updateStatus(ids,status);
    }


    /**
     * 或许虚拟商品
     * @return
     */
    @RequestMapping("selectShopData")
    public  String selectShopData(Model model)
    {
        model.addAttribute("courseTypeList", CourseType.values());
        return  resultPage("/integralgood/select_shopData");
    }

    /**
     * 查看兑换详情
     * @return
     */
    @RequestMapping("integralRecord")
    public  String integralRecord(Model model, @RequestParam BigDecimal id){

        Map byIdEntity = integralGoodService.findByIdEntity(id, true);
        model.addAttribute("entity",byIdEntity);
        return  resultPage("/integralgood/good_info");
    }

    /**
     * 查询
     * @param model
     * @param condition
     * @param currentPage
     * @return
     */
    @RequestMapping("integralExchangeRecordList")
    public String integralExchangeRecordList(Model model,
                                                 @RequestBody IntegralExchangeRecordExpand condition,
                                                 @RequestParam("currentPage") int currentPage){
        condition.setSetUser(true);
        Map listPage = integralExchangeRecordService.findListPage(currentPage, condition);
        model.addAttribute("dataMap",listPage);
        return resultPage("/integralgood/ajax_good_exchang_info");
    }

    /**
     * 发货
     * @param condition
     * @return
     */
    @RequestMapping("toSendExpress")
    @ResponseBody
    public Map toSendExpress(HttpServletRequest request,@RequestBody Map condition){
        condition.put("loginUserId",loginUtil.getLoginUserId(request));
        integralExchangeRecordService.toSendExpress(condition);
        return ResultUtil.SUCCESS("发货成功");
    }


}
